#!/bin/bash
echo "开始安装"
## 卸载自带的jdk
rpm -qa | grep java | xargs rpm -e --nodeps
#存放jdk，压缩包的路径
JDK_HOME_TAR="/home/option/jdk"
#jdk包的名称
JDK_NAME_TAR="jdk-8u181-linux-x64.tar.gz"
#jdk文件夹名称
JDK_NAME="jdk1.8.0_181"
#安装目录
JDK_HOME_WILL="/usr/local"
cd $JDK_HOME_WILL
#检测目录是否存在
if [  -d "java" ];then
        echo "目录已存在，删除"
        rm -rf "java"
fi
cp "$JDK_HOME_TAR"/"$JDK_NAME_TAR" "$JDK_HOME_WILL"  && echo "复制完成"
cd $JDK_HOME_WILL && echo "进入到目录成功 $JDK_HOME_WILL"
echo "解压中" && tar -zxf "$JDK_NAME_TAR" && echo "解压完成"
mv "$JDK_NAME" java && echo "改名完成"
rm -rf $JDK_NAME_TAR && echo "删除安装包成功"
sed -i '$a export JAVA_HOME='$JDK_HOME_WILL'/java' /etc/profile
sed -i '$a export CLASSPATH=$:CLASPATH:$JAVA_HOME/lib/' /etc/profile
sed -i '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile && echo "添加配置>信息成功"
source /etc/profile && echo "环境变量生效"